package leetcode_core.leetcode_2;

public class RemoveNthFromEnd {

    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        //根据题目样例说明,不会为空
        ListNode slow = head,fast = head;
        //然后fast先走k步
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }
        //由于要在目的节点之前停下来,所以fast走到最后一个节点就可以了,而不必走到null
        while (fast.next!=null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return head;
    }



}
